package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.QueryPageBean;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Goods;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.service.SpuService;
import com.changgou.goods.pojo.Spu;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/spu")
public class SpuController {


    @Autowired
    private SpuService spuService;

    /**
     * 查询全部数据
     *
     * @return
     */
    @GetMapping
    public Result<List<Spu>> findAll() {
        List<Spu> spuList = spuService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", spuList);
    }

//    //查询所有商品
//    @GetMapping("/findAll")
//    public Result<List<Goods>> findAllGoods() {
//        List<Goods> allGoods = spuService.findAllGoods();
//        return new Result(true, StatusCode.OK, "查询成功", allGoods);
//        //todo
//    }


    /***
     * 根据ID查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
//        Spu spu = spuService.findById(id);
        Goods goods = spuService.findGoodsById(id);
        return new Result(true, StatusCode.OK, "查询成功", goods);
    }

    @GetMapping("/findSpuById/{id}")
    public Result<Spu> findSpuById(@PathVariable String id) {
        Spu spu = spuService.findById(id);

        return new Result(true, StatusCode.OK, "查询成功", spu);
    }

    /***
     * 新增数据
     * @param goods
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Goods goods) {
        spuService.add(goods);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /***
     * 修改数据
     * @param goods
     *
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Goods goods) {
        spuService.update(goods);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable("id") String id) {
        try {
            spuService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "当前商品必须处于下架状态才能删除");
        }
        return new Result(true, StatusCode.OK, "删除成功");
    }

    /***
     * 多条件搜索品牌数据
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search")
    public Result findList(@RequestParam Map searchMap) {
        List<Spu> list = spuService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功", list);
    }


    /***
     * 分页搜索实现
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page<Spu> pageList = spuService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    // 审核商品
    @PutMapping("/audit/{id}/{status}")
    public Result audit(@PathVariable("id") String id, @PathVariable("status") String status) {
        spuService.audit(id, status);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    //    上架
    @PutMapping("/marketable/{id}/{marketable}")
    public Result marketable(@PathVariable("id") String id, @PathVariable("marketable") String marketable) {
        try {
            spuService.marketable(id, marketable);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR, "未通过审核的商品不能上架！");
        }
        return new Result(true, StatusCode.OK, "操作成功");
    }

    //还原商品
    @PutMapping("/restore/{id}")
    public Result restore(@PathVariable("id") String id) {
        spuService.restore(id);
        return new Result(true, StatusCode.OK, "商品换原成功");
    }

    //从数据库删除该商品
    @DeleteMapping("/realDel/{id}")
    public Result realDel(@PathVariable("id") String id) {
        spuService.realDel(id);
        return new Result(true, StatusCode.OK, "商品删除成功");
    }

//    //查询回收站商品
//    @RequestMapping("/findReGoods")
//    public Result findReGoods(@RequestBody QueryPageBean queryPageBean) {
//        List<Spu> reGoods = spuService.findReGoods();
//        return new Result(true, StatusCode.OK, "查询成功", reGoods);
//    }
    //    分页查询回收站商品
    //查询回收站商品
    @RequestMapping("/findReGoodsPage")
    public Result findReGoodsPage(@RequestBody QueryPageBean queryPageBean) {
        Integer page = queryPageBean.getCurrentPage();
        Integer size = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if (queryString != null) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", queryString);
            Page<Spu> reGoods = spuService.findReGoods(map,page, size);
            PageResult<Spu> result = new PageResult<>(reGoods.getTotal(), reGoods.getResult());
            return new Result(true, StatusCode.OK, "查询成功", result);
        }
        Page<Spu> reGoods = spuService.findReGoods(page, size);
        PageResult<Spu> result = new PageResult<>(reGoods.getTotal(), reGoods.getResult());
        return new Result(true, StatusCode.OK, "查询成功", result);
    }

    //分页商品查询
    @RequestMapping("/findPage1")
    public Result findPage1(@RequestBody QueryPageBean queryPageBean) {
        Integer page = queryPageBean.getCurrentPage();
        Integer size = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        if (queryString != null) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", queryString);
//            map.put("name", queryString);
            Page<Spu> page1 = spuService.findPage(map, page, size);
            PageResult<Spu> result = new PageResult<>(page1.getTotal(), page1.getResult());
            return new Result(true, StatusCode.OK, "查询成功", result);

        }
        Page<Spu> page1 = spuService.findPage(page, size);

        PageResult<Spu> result = new PageResult<>(page1.getTotal(), page1.getResult());
        return new Result(true, StatusCode.OK, "查询成功", result);

    }

//    @RequestMapping("/query")
//    public Result query(@RequestBody QueryPageBean queryPageBean) {
//        List<Spu> spuList = spuService.query(queryPageBean);
//        return new Result(true, StatusCode.OK, "查询成功", spuList);
//    }


}
